package com.hqyj.service.impl;


import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.hqyj.dao.UserMapper;
import com.hqyj.dao.UserRoleMapper;
import com.hqyj.pojo.Role;
import com.hqyj.pojo.User;
import com.hqyj.pojo.UserRole;
import com.hqyj.service.UserService;
import com.hqyj.util.MdFive;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.IncorrectCredentialsException;
import org.apache.shiro.authc.UnknownAccountException;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;

/**
 * <p></p>
 *
 * @author : hyq
 * @date : 2020-09-01 16:09
 **/
@Service
public class UserServiceImpl implements UserService {
    //注入密码加密工具类
    @Autowired
    MdFive mdFive;
    @Autowired
    UserMapper userMapper;
    @Autowired
    UserRoleMapper userRoleMapper;

    @Override
    public String login(User a, HashMap<String,Object> map) {
        try{
            //创建主体，即访问应用的用户，在 Shiro 中使用 Subject 代表该用户
            Subject subject = SecurityUtils.getSubject();
            //创建用户凭证，即当前登录用户名和密码对象
            UsernamePasswordToken token = new UsernamePasswordToken(a.getUserName(), a.getUserPwd());
            //登录，即身份验证
            subject.login(token);
        }catch (UnknownAccountException e){
            e.printStackTrace();
            System.err.println("用户名不存在");
            map.put("info","用户名不存在");
            return "login";
        }catch (IncorrectCredentialsException e){
            e.printStackTrace();
            System.err.println("密码不正确");
            map.put("info","密码不正确");
            return "login";
        }
        return "index";
    }
    //注册
    @Override
    public HashMap<String,Object> insertSelective(User record) {

        return null;
    }


    @Override
    public HashMap<String, Object> add(User user) {
        HashMap<String, Object> map = new HashMap<String, Object>();
        //加密用户注册的密码
        String pwd = mdFive.encrypt(user.getUserPwd(), user.getUserName());
        //把加密的密码存入user对象中
        user.setUserPwd(pwd);
        if(userMapper.insertSelective(user)>0){
            map.put("info","新增成功");
        }else{
            map.put("info","新增失败");
        }
        return map;
    }

    @Override
    public HashMap<String, Object> update(User user) {
        HashMap<String, Object> map = new HashMap<String, Object>();
        //加密用户注册的密码
        String pwd = mdFive.encrypt(user.getUserPwd(), user.getUserName());
        //把加密的密码存入user对象中
        user.setUserPwd(pwd);
        if(userMapper.updateByPrimaryKeySelective(user)>0){
            map.put("info","修改成功");
        }else{
            map.put("info","修改失败");
        }
        return map;
    }

    @Override
    public HashMap<String, Object> del(Integer id) {
        HashMap<String, Object> map = new HashMap<String, Object>();
        if(userMapper.deleteByPrimaryKey(id)>0){
            map.put("info","删除成功");
        }else{
            map.put("info","删除失败");
        }
        return map;
    }

    @Override
    public HashMap<String, Object> select(User user) {
        HashMap<String, Object> map = new HashMap<String, Object>();
        //1 设置分页参数：页码和条数
        PageHelper.startPage(user.getPage(),user.getRow());
        //2 查询结果集合
        List<User> list = userMapper.select();
        // 3 创建分页对象
        PageInfo<User> pageInfo = new PageInfo<User>(list);

        System.out.println("总条数="+pageInfo.getTotal());
        System.out.println("总页数="+pageInfo.getPages());
        System.out.println("上一页="+pageInfo.getPrePage());
        System.out.println("下一页="+pageInfo.getNextPage());
        System.out.println("首页="+pageInfo.getNavigateFirstPage());
        System.out.println("尾页="+pageInfo.getNavigateLastPage());
        System.out.println("当前页="+user.getPage());
        List<User> listResult = pageInfo.getList();
        for(User e:listResult){
            System.out.println(e.toString());
        }
        map.put("total",pageInfo.getTotal());
        map.put("pages",pageInfo.getPages());
        map.put("endPage",pageInfo.getNavigateLastPage());
        map.put("curPage",user.getPage());
        map.put("data",pageInfo.getList());
        return map;
    }


    @Override
    public HashMap<String, Object> fpRole(UserRole userRole) {
        HashMap<String, Object> map = new HashMap<String, Object>();
        if(userRoleMapper.insertSelective(userRole)>0){
            map.put("info","分配成功");
        }else{
            map.put("info","分配失败");
        }
        return map;
    }

    @Override
    public HashMap<String, Object> selectRole(User user) {
        HashMap<String, Object> map = new HashMap<String, Object>();
        List<String> listRole=userMapper.selectRole(user.getUserName());
        map.put("x",listRole);
        return map;
    }

    @Override
    public HashMap<String, Object> xgRole(UserRole userRole) {
        HashMap<String, Object> map = new HashMap<String, Object>();
        List<UserRole> userRoles = userRoleMapper.select(userRole);
        if (userRoles.size()==0){
            userRoleMapper.deleteByPrimaryKey(userRole.getuId());
            userRoleMapper.insertSelective(userRole);
        }else {
            for (UserRole userrole : userRoles) {
                userRoleMapper.deleteByPrimaryKey(userRole.getuId());
                if(userRoleMapper.insertSelective(userRole)>0){
                    map.put("info","修改成功");
                }else{
                    map.put("info","修改失败");
                }
            }
        }

        return map;
    }
}
